// pages/visitingCardSet/visitingCardSet.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    haveChoosedUserIcon:false,
    weixin: '',
    dizhi: '',
    phone: '',
    email: '',
    company:'',
    username:'',
    flag1:0,
    flag2:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let typeId = options.modelType
    var that = this
    var a=wx.getStorageSync("userInfo");
         that.setData({
            nickname: a.NickName
          })
        //获得二维码，头像存在本地
        wx.downloadFile({
            url: a.HeadUrl,
            success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                  that.setData({
                      tou: res.tempFilePath,
                      flag1:1,
                  })
              }
            }
         })
    that.fenxiang()
    that.posterinfo()
    console.log(that.data)
  },
  fenxiang: function() {
     var that =this;
       wx.request({
        url: app.d.ceshiUrl + '/Api/Bshare/index',
        method: 'post',
        data: {
          uid: app.d.userId,
          pid: 0,
          store_id: 0,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            var data = res.data;
            wx.downloadFile({
            url: data.img,
            success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                  that.setData({
                      erweima: res.tempFilePath,
                      flag2:1,
                  })
              }
            }
         })
        },
        fail: function() {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        }
      });
  },
  posterinfo: function() {
     var that =this;
     wx.request({
          url: app.d.ceshiUrl + '/Api/Poster/poster',
          data: {
            uid:  app.d.userId,
          },
          method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            let rest = res.data;

            if(rest.status==1){
              setTimeout(function() {
                rest.data.tou=that.data.tou;
                rest.data.erweima=that.data.erweima;
                rest.data.nickname=that.data.nickname;
                console.log(rest)
                wx.setStorageSync("tempuser", rest.data)  
              }.bind(this), 1500)
            
              that.setData({
                 weixin: rest.data.name,
                 dizhi: rest.data.address,
                 phone: rest.data.phone,
                 email: rest.data.email,
                 company:rest.data.company,
                 username:rest.data.username
              })
            } 
          }
        })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 生成名片
   */
  toBuildVisitingCard(){
    var that = this
    console.log(that.data)
    if(!that.data.weixin){
      wx.showToast({
        title: '请输入您的微信',
        icon: 'none'
      })
      return
    }
    if (!that.data.dizhi) {
      wx.showToast({
        title: '请输入公司地址',
        icon: 'none'
      })
      return
    }
    if (!that.data.phone) {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      })
      return
    }
    if (!that.data.email) {
      wx.showToast({
        title: '请输入联系邮箱',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: app.d.ceshiUrl + '/Api/Poster/posterinfo',
      data: {
        uid:  app.d.userId,
        name: that.data.weixin,
        address:  that.data.dizhi,
        phone:  that.data.phone,
        email:  that.data.email,
        company:that.data.company,
        username:that.data.username
      },
      method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        let rest = res.data;
        rest.data.tou=that.data.tou;
        rest.data.erweima=that.data.erweima;
        rest.data.nickname=that.data.nickname;
        wx.setStorageSync("tempuser", rest.data)
        if(rest.status==1&&that.data.flag1==1&&that.data.flag2==1){
          wx.showToast({
            title: '去制作',
            duration: 2000
          });
          setTimeout(function() {
              wx.navigateTo({
                url: '/pages/visitingCardProduct/visitingCardProduct', 
              })
          }.bind(this), 2000)
        }else{
           wx.showToast({
            title: '稍等片刻,正在下载二维码',
            duration: 2000,
            icon: 'none'
          });
        }
      }
    })
  },

  /**
   * 填写信息
   */
  textFill(e){
    let typeName = e.currentTarget.id 
    let userInfo = e.detail.value
    var that = this
    console.log(userInfo)
    if(typeName == 'name'){
      that.setData({
        name:userInfo
      })
    }else if(typeName == 'address'){
      that.setData({
        address:userInfo
      })
    }else if(typeName == 'phone'){
      that.setData({
        phone:userInfo
      })
    }else {
      that.setData({
        email:userInfo
      })
    }
  },
  userWeixInput:function(e)
  {
    this.setData({
      weixin: e.detail.value
    })
  },
  userGongsInput:function(e)
  {
    this.setData({
      dizhi: e.detail.value
    })
  },
  userPhotosInput:function(e)
  {
    this.setData({
      phone: e.detail.value
    })
  },
  userEmailInput:function(e)
  {
    this.setData({
      email: e.detail.value
    })
  },
  usernameInput:function(e)
  {
    this.setData({
      username: e.detail.value
    })
  },
  companyInput:function(e)
  {
    this.setData({
      company: e.detail.value
    })
  },
})